<?php

/**
 * This is the model class for table "announcement_assign".
 *
 * The followings are the available columns in table 'announcement_assign':
 * @property integer $member_id
 * @property integer $announcement_id
 * @property integer $purpose_join
 * @property integer $event_joined
 */
class AnnouncementAssign extends AnnouncementAssignBase
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return AnnouncementAssign the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'member_id' => 'Thành viên',
			'announcement_id' => 'Thông báo',
			'purpose_join' => 'Dự kiến tham dự',
			'event_joined' => 'Đã tham dự',
		);
	}

	public function getAsignNew($memberId, $eventFg = "") {
		$criteria 	= new CDbCriteria();
		$criteria->with			= array('announcement' => array('select' => 'title, id', 'condition' => 'status = 1'));
        $criteria->order		= 'announcement.created DESC';
		//$criteria->condition	= 't.event_joined = 0 AND t.purpose_join = 0 AND member_id = :memberId';
		$criteria->condition	= 'ADDDATE(created, INTERVAL 3 MONTH) >= NOW() AND member_id = :memberId';
		$criteria->params		= array(':memberId' => $memberId);
		if ($eventFg != "") {
			$criteria->addCondition("event_fg = :event_fg");
			$criteria->params[":event_fg"]		= $eventFg;
		}

		return $this->findAll($criteria);
	}

	public function countJoin($id, $type = 2) {
		// Get join
        $criteria 	= new CDbCriteria();
        $criteria->addCondition("t.announcement_id = :announcement_id");
        $criteria->addCondition('t.purpose_join = :purpose_join');
        $criteria->params = array(
        	':announcement_id' 	=> $id,
        	':purpose_join'		=> $type
        );

		return $this->count($criteria);
	}

	public function checkAsign($postId, $userId = null) {
		$check 	= false;
		if ($postId != '' && $userId != '') {
			$result = $this->findByAttributes(array('announcement_id' => $postId, 'member_id' => $userId, 'event_joined' => 0));
			if (isset($result)) {
				$check = ($result->purpose_join == 0) ? true : $result->purpose_join;
			}
		}
		return $check;
	}

	public function setAsign($postId, $userId, $flag = 2, $comment = '') {
		if ($postId != '' && $userId != '') {
			$this->updateAll(array('purpose_join' => $flag, 'comment' => $comment),
				'announcement_id = :postId AND member_id = :userId',
				array(':postId' => $postId, ':userId' => $userId)
			);
		}
	}

	public function countView($id) {
		return $this->countByAttributes(array('announcement_id' => $id, 'view_fg' => 1));
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('member_id',$this->member_id);
		$criteria->compare('announcement_id',$this->announcement_id);
		$criteria->compare('purpose_join',$this->purpose_join);
		$criteria->compare('event_joined',$this->event_joined);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}